package cn.xyy.common.algorithm;

import cn.xyy.common.domain.system.auth.MenuMetaVO;
import cn.xyy.common.domain.system.auth.MenuVO;
import cn.xyy.common.domain.system.auth.Permission;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 菜单算法类
 * 
 * @author xyy
 *
 */
public class MenuAlgorithm {

	/**
	 * 递归将数据tree存储结构
	 *
	 * @param @param  allList
	 * @param @return 设定文件
	 * @return List<Department> 返回类型
	 *
	 */
	public static List<Permission> tree(List<Permission> allList) {
		if (allList == null || allList.isEmpty()) {
			return null;
		}
		Collections.sort(allList, (s1, s2) -> s1.getSorts().compareTo(s2.getSorts()));

		List<Permission> tree = new ArrayList<Permission>();
		for (Permission node : allList) {
			if (node.isRoot()) {
				// 遍历根节点
				buildTreeNode(node, allList);
				tree.add(node);
			}
		}
		return tree;
	}

	/**
	 * 构建Node
	 * 
	 * @param node
	 */
	private static void buildTreeNode(Permission node, List<Permission> allList) {
		List<Permission> childrens = null;
		for (Permission child : allList) {
			// 获取子节点
			if (node.getId().equals(child.getParentId())) {
				if (null == childrens) {
					childrens = new ArrayList<Permission>();
				}
				childrens.add(child);
				buildTreeNode(child, allList);
			}
		}
		if (null != childrens && !childrens.isEmpty()) {
			Collections.sort(childrens, (s1, s2) -> s1.getSorts().compareTo(s2.getSorts()));
			node.setChildren(childrens);
		}
	}

	/**
	 * 构建菜单
	 *
	 * @param @param  menus
	 * @param @return 设定文件
	 * @return List<MenuVO> 返回类型
	 *
	 */
	public static List<MenuVO> buildMenu(List<Permission> menuList) {
		if (menuList == null || menuList.isEmpty()) {
			return null;
		}

		List<MenuVO> treeMenuList = new ArrayList<MenuVO>();
		for (Permission menu : menuList) {
			List<Permission> childrenList = menu.getChildren();
			MenuVO menuVo = new MenuVO();
			menuVo.setPath(menu.getPath());
			menuVo.setMeta(new MenuMetaVO(menu.getTitle(), menu.getIcon(), menu.getCache() == 2 ? true : false,
					menu.getHide() == 1 ? true : false, menu.getHideChildren() == 1 ? true : false));
			menuVo.setRoot(("0".equals(menu.getParentId())) ? true : false);
			menuVo.setComponent(menu.getComponent());
			if ("0".equals(menu.getParentId()) || menu.getTypes() == 0) {
				// 一级菜单
				if (menu.getIframe() == 2) {
					menuVo.setName(menu.getName());
					menuVo.setRedirect(menu.getRedirect());
					menuVo.setComponent("LAYOUT");

				} else {
					// 外链
					menuVo.setComponent("IFrame");
				}
			} else {
				// 二级菜单
				menuVo.setName(menu.getName());
				menuVo.setComponent(menu.getComponent());
			}
			if (childrenList != null && !childrenList.isEmpty()) {
				// 子菜单
				menuVo.setChildren(buildMenu(childrenList));

			} else if ("0".equals(menu.getParentId())) {
//				// 处理是一级菜单并且没有子菜单的情况
//				MenuVO menuVo1 = new MenuVO();
//				menuVo1.setMeta(menuVo.getMeta());
//				// 非外链
//				if (menu.getI_frame() == 0) {
//					menuVo.setComponent("LAYOUT");
//					menuVo.setTitle(menu.getTitle());
//					menuVo1.setName(menu.getComponent());
//					menuVo1.setPath(menu.getPath());
//					menuVo1.setComponent(menu.getComponent());
//					menuVo1.setRoot((menu.getParentId() == 0) ? true : false);
//				} else {
//					menuVo1.setPath(menu.getPath());
//				}
//				List<MenuVO> list1 = new ArrayList<MenuVO>();
//				list1.add(menuVo1);
//
//				menuVo.setName(null);
//				menuVo.setMeta(null);
//				menuVo.setChildren(list1);
			} else {
				menuVo.setChildren(new ArrayList<MenuVO>());
			}
			treeMenuList.add(menuVo);
		}
		return treeMenuList;
	}

}
